package http_tunnel;

public class HttpTunneling {
	
	private String dest;
	private int window;
	private String data;
	private int port = -1;
	
	private String httpVersion;
	
	/*
	private Pattern [][] code = {
			{Pattern.compile(" "), Pattern.compile("\\&esp\\$")}, 
			{Pattern.compile("\\\n"), Pattern.compile("\\&bslashn\\$")},
			{Pattern.compile("\\\r"), Pattern.compile("\\&bslashr\\$")}
			};*/
	
	public HttpTunneling(){
		dest = "";
		window = -1;
		port = -1;
		data = "";
		httpVersion = "";
		
	}
	
	public String composeHttpRequest(String type, String emmeteur, String dest, int port, int window, String data, String httpVersion){
		/*TODO: Encodage des elements de la requete*/
		data = encode(data);
		
		/*Creation de la requete*/
		//ex : GET CLIENT/dest=hoteTier/port=1234/window=2/data=Test_de_donnees_envoyees HTTP/1.0\r\n
		String request = "";
		
		if (type.equals("GET")){
			request = type + " ";
		}
		else{
			return null;//TODO exception bad http request
		}
		
		if (emmeteur.equals("CLIENT") || emmeteur.equals("SERVER")){
			request += emmeteur;
		}
		else{
			return null;//TODO exception bad http request
		}

		request += "/dest=" + dest;
		request += "/port=" + port;
		
		if(emmeteur.equals("CLIENT")){ //Valable uniquement pour le client
			if(window >= 0){
				request += "/window=" + window;
			}
			else{
				return null;//TODO exception bad http request
			}
		}
		
		request += "/data=" + data;
		
		request += " " + httpVersion;
		
		return request;
	}
	
	public void decomposeHttpRequest(String request){
		int debut;
		int fin;
		
		/*Decomposition*/
		//ex : GET CLIENT/dest=hoteTier/port=1234/window=2/data=Test_de_donnees_envoyees HTTP/1.0\r\n
		
		debut = request.indexOf("/dest=") + 6;
		fin = request.indexOf("/port=");
		dest = request.substring(debut, fin);
	
		debut = request.indexOf("/port=") + 6;
		fin = request.indexOf("/window=");
		port = Integer.parseInt(request.substring(debut, fin));
		
		debut = request.indexOf("/window=") + 8;
		fin = request.indexOf("/data=");
		window = Integer.parseInt(request.substring(debut, fin));
		
		httpVersion = request.substring(request.indexOf(" HTTP/"));
		
		debut = request.indexOf("/data=") + 6;
		fin = request.length() - getHttpVersion().length();
		data = request.substring(debut, fin);
		
		/*Decodage*/
		data = decode(data);
	}

	public String encode(String normalString){
		String codedString = normalString;

		codedString = codedString.replaceAll(" ", "\\&esp\\$");
		codedString = codedString.replaceAll("\\\n", "\\&bslashn\\$");
		codedString = codedString.replaceAll("\\\r", "\\&bslashr\\$");
		
		return codedString;
	}

	public String decode(String codedString){
		String normalString = codedString;
		
		normalString = normalString.replaceAll("\\&esp\\$", " ");
		normalString = normalString.replaceAll("\\&bslashn\\$", "\\\n");
		normalString = normalString.replaceAll("\\&bslashr\\$", "\\\r");
		
		return normalString;
	}
	
	public String getHttpVersion(){
		return httpVersion;
	}
	
	public String getDest(){
		return dest;
	}
	
	public int getWindow(){
		return window;
	}

	public int getPort(){
		return port;
	}
	
	public String getData(){
		return data;
	}
}